<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;


// use Crypt;
use Hash;

use DB;

class LoginController extends Controller
{

    //login 登录页面
    public function login()
    {
    	$title = '后台管理';
    	return  view('admin.login.login',['title' => $title]);
    }

    //dologin 处理登录
    public function dologin(Request $request)
    {
    	$data = $request -> except('_token');
 
    	$this->validate($request, [
            'username' => 'required|max:18',
            'password' => 'required',
        ],[
            'username.required' => '用户名不能为空',
            'username.max' => '用户名不能超过18',
            'password.required' => '密码不能为空',
        ]);

         
        $users = DB::table('bro_staff') -> where('username',$data['username']) -> first();

        if(!empty($users))
        {
            $p = DB::table('bro_staff') -> where('username',$data['username']) -> first() -> password;
            if (Hash::check($data['password'], $p)) {
                session(['staff' => $users]);
                switch($users -> group)
                {
                    case 0:
                        switch($users -> status)
                        {
                            case 0:
                            // session(['user' => $users]);
                            return redirect('/admin/index/index') -> with(['success' => '登录成功']);
                            case 1:
                            // session(['user' => $users]);
                            return back() -> with(['error' => '您的权限已被禁止,请联系超管']);
                        }
                        break;
                    case 1:
                        // session(['user' => $users]);
                        return redirect('/admin/index/index') -> with(['success' => '登录成功']);
                        break;
                }

                }else {
                    return back() -> with(['error' => '登录失败,密码错误']);
                }

        }else{
             return back() -> with(['error' => '登录失败,用户名或者密码错误']);
        }
     }


     //退出
    public function quit()
    {
        session(['staff' => null]);
        return redirect('admin/login/login');
    }

}
